#!/usr/bin/python

from gnuradio import gr
from gnuradio.gr import firdes
from gnuradio.qtgui import qtgui
from PyQt4 import Qt
import sys
import sip

sys.path.insert(0, './python')

import laps_gui

class trx(gr.top_block):
	enable_gui = True
	samp_rate = 32000

	def __init__(self, args):
		gr.top_block.__init__(self, "Test Gui")

		self.gui = laps_gui.gui("Test GUI", args, self)
		self.top_layout = self.gui.get_top_layout()

		# Tabs Creation
		tab_names = ['Test Tab']
		self.gui_layout = self.gui.create_tab_widget(tab_names, self.top_layout, [])

		##################################################
		# Blocks
		##################################################
		self.up_converter_0 = up_converter(
			samp_rate=32e3,
			base_freq=5e3,
		)
		self.random_source_x_0 = gr.vector_source_b(map(int, numpy.random.randint(0, 255, 1000)), True)
		self.gr_throttle_0 = gr.throttle(gr.sizeof_gr_complex*1, samp_rate)
		self.gr_null_sink_0 = gr.null_sink(gr.sizeof_char*1)
		self.down_converter_0 = down_converter(
			cutoff_freq=2.5e3,
			base_freq=5e3,
			samp_rate=32e3,
			beta=3.4,
		)
		self.digital_qam_mod_0 = digital.qam.qam_mod(
		  constellation_points=4,
		  mod_code="gray",
		  differential=True,
		  samples_per_symbol=16,
		  excess_bw=0.35,
		  verbose=False,
		  log=False,
		  )
		self.digital_qam_demod_0 = digital.qam.qam_demod(
		  constellation_points=4,
		  differential=True,
		  samples_per_symbol=2,
		  excess_bw=0.35,
		  freq_bw=6.28/100.0,
		  timing_bw=6.28/100.0,
		  phase_bw=6.28/100.0,
		  mod_code="gray",
		  verbose=False,
		  log=False,
		  )



		a = gr.vector_source_b([1,0,1], True, 1)
		thr = gr.throttle(gr.sizeof_char, self.samp_rate)
		b = gr.null_sink(gr.sizeof_char)
		self.connect(a, thr, b)

		c2f = gr.char_to_float()
		gui_a = qtgui.time_sink_f( 512, self.samp_rate, "", 1)
		self.connect(a, c2f, gui_a)

		##################################################
		# Connections
		##################################################
		self.connect((self.digital_qam_demod_0, 0), (self.gr_null_sink_0, 0))
		self.connect((self.down_converter_0, 0), (self.digital_qam_demod_0, 0))
		self.connect((self.random_source_x_0, 0), (self.digital_qam_mod_0, 0))
		self.connect((self.digital_qam_mod_0, 0), (self.gr_throttle_0, 0))
		self.connect((self.up_converter_0, 0), (self.down_converter_0, 0))
		self.connect((self.gr_throttle_0, 0), (self.up_converter_0, 0))


		gui_win = sip.wrapinstance(gui_a.pyqwidget(), Qt.QWidget)
		self.gui_layout[0].addWidget(gui_win)

if __name__ == '__main__':
	tb = trx(sys.argv) # Create trx object class
	tb.start() #  start gnuradio flow-graph
	tb.gui.start() # now just run gui application
	tb.stop()



